package com.zhaoyue.study.concurrency.queue.delayqueuedemo;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2016/12/8
 * Time: 17:43
 * To change this template use File | Settings | File Templates.
 */
public class Food implements Delayed {

    private String foodName;
    private long saveTime;
    private long expireTime;


    public Food(String foodName,long saveTime){
        this.foodName = foodName;
        this.saveTime = saveTime;
        this.expireTime = TimeUnit.NANOSECONDS.convert(saveTime,TimeUnit.SECONDS) + System.nanoTime();
    }




    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expireTime - System.nanoTime(),TimeUnit.NANOSECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        Food food = (Food) o;
        if (this.expireTime > food.expireTime) {
            return 1;
        } else if (this.expireTime == food.expireTime) {
            return 0;
        } else {
            return -1;
        }
    }

    public String getFoodName() {
        return foodName;
    }

    public void setFoodName(String foodName) {
        this.foodName = foodName;
    }

    public long getSaveTime() {
        return saveTime;
    }

    public void setSaveTime(long saveTime) {
        this.saveTime = saveTime;
    }

    public long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(long expireTime) {
        this.expireTime = expireTime;
    }
}
